var t = new (require("../../libs/qqmap-wx-jssdk.min.js"))({
    key: "NEHBZ-IP7R4-WCAUL-DC5EM-2O652-MVBAP"
}), e = require("../../utils/config.js");

Page({
    data: {
        address: "",
        imageUrl: "",
        visitObject: "",
        visitMode: "",
        mainMatters: "",
        visitResult: "",
        remark: "",
        lat: 0,
        lng: 0,
        visitModeList: [ "上门", "电话" ],
        isHideMore: !1
    },
    onLoad: function(t) {
        if (t.obj) {
            console.log(t.obj);
            var a = JSON.parse(t.obj);
            this.setData({
                address: a.address,
                visitObject: a.visitObject,
                visitMode: a.visitMode,
                mainMatters: a.mainMatters,
                visitResult: a.visitResult,
                remark: a.remark,
                isHideMore: !0
            }), a.imageId && this.setData({
                imageUrl: e.urlList.API_TOOLS_GET_FILE + a.imageId
            });
        } else this.getAddress();
    },
    getAddress: function() {
        var e = this;
        wx.getLocation({
            type: "gcj02",
            success: function(a) {
                t.reverseGeocoder({
                    location: {
                        latitude: a.latitude,
                        longitude: a.longitude
                    },
                    success: function(t) {
                        console.log(t), e.setData({
                            address: t.result.formatted_addresses.recommend,
                            lng: t.result.location.lng,
                            lat: t.result.location.lat
                        });
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    getVisitObject: function(t) {
        this.setData({
            visitObject: t.detail.value
        });
    },
    getVisitMode: function(t) {
        this.setData({
            visitMode: t.detail.value
        });
    },
    getMainMatters: function(t) {
        this.setData({
            mainMatters: t.detail.value
        });
    },
    getVisitResult: function(t) {
        this.setData({
            visitResult: t.detail.value
        });
    },
    getRemark: function(t) {
        this.setData({
            remark: t.detail.value
        });
    },
    selectedVisitMode: function(t) {
        var e = this;
        e.setData({
            visitMode: e.data.visitModeList[t.detail.value]
        });
    },
    previewImage: function(t) {
        var e = this;
        e.data.imageUrl && wx.previewImage({
            current: t.target.dataset.src,
            urls: [ e.data.imageUrl ]
        });
    },
    chooseImage: function() {
        var t = this;
        wx.chooseImage({
            count: 1,
            success: function(e) {
                console.log(e), t.setData({
                    imageUrl: e.tempFilePaths[0]
                });
            }
        });
    },
    apply: function() {
        var t = this;
        if (t.data.visitObject) if (t.data.visitMode) if (t.data.mainMatters) if (t.data.visitResult) {
            wx.showLoading({
                mask: !0
            });
            var a = wx.getStorageSync("userId"), i = wx.getStorageSync("serviceProviderId");
            if (t.data.imageUrl) wx.uploadFile({
                url: e.urlList.API_TOOLS_UPLOAD_FILE,
                filePath: t.data.imageUrl,
                name: "file",
                success: function(s) {
                    var o = JSON.parse(s.data);
                    if (200 == o.status) {
                        var n = {
                            userId: a,
                            serviceProviderId: i,
                            address: t.data.address,
                            lng: t.data.lng,
                            lat: t.data.lat,
                            remark: t.data.remark,
                            imageId: o.data.fileId,
                            visitObject: t.data.visitObject,
                            visitMode: t.data.visitMode,
                            mainMatters: t.data.mainMatters,
                            visitResult: t.data.visitResult
                        };
                        console.log(n), wx.request({
                            url: e.urlList.API_VISIT_LOG_ADD,
                            method: "POST",
                            header: {
                                contentType: "application/json"
                            },
                            data: n,
                            success: function(t) {
                                wx.hideLoading(), 200 == t.data.status ? (wx.navigateBack(), wx.showToast({
                                    title: "新增成功"
                                })) : wx.showToast({
                                    title: t.data.message,
                                    icon: "none"
                                });
                            }
                        });
                    } else wx.hideLoading(), wx.showToast({
                        title: s.data.message,
                        icon: "none"
                    });
                }
            }); else {
                var s = {
                    userId: a,
                    serviceProviderId: i,
                    address: t.data.address,
                    lng: t.data.lng,
                    lat: t.data.lat,
                    remark: t.data.remark,
                    imageId: "",
                    visitObject: t.data.visitObject,
                    visitMode: t.data.visitMode,
                    mainMatters: t.data.mainMatters,
                    visitResult: t.data.visitResult
                };
                console.log(s), wx.request({
                    url: e.urlList.API_VISIT_LOG_ADD,
                    method: "POST",
                    header: {
                        contentType: "application/json"
                    },
                    data: s,
                    success: function(t) {
                        wx.hideLoading(), 200 == t.data.status ? (wx.navigateBack(), wx.showToast({
                            title: "新增成功"
                        })) : wx.showToast({
                            title: t.data.message,
                            icon: "none"
                        });
                    }
                });
            }
        } else wx.showToast({
            title: "请输拜访结果",
            icon: "none"
        }); else wx.showToast({
            title: "请输入主要事宜",
            icon: "none"
        }); else wx.showToast({
            title: "请选择拜访方式",
            icon: "none"
        }); else wx.showToast({
            title: "请输入拜访对象",
            icon: "none"
        });
    },
    goToVisitHistory: function() {
        wx.navigateTo({
            url: "visitHistory"
        });
    }
});